package com.skyjing.testadapter

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewbinding.ViewBinding
import com.loxo.testadapter.databinding.ActivityMainBinding
import com.loxo.testadapter.databinding.ItemABinding
import com.loxo.testadapter.databinding.ItemBBinding
import com.loxo.testadapter.databinding.ItemMainBinding
import com.skyjing.rvadapter.CommonAdapter
import com.skyjing.rvadapter.MultiItemCommonAdapter
//import com.loxo.rvadapter.MultiItemCommonAdapter
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private var list: MutableList<String> = ArrayList()
    private var list2: MutableList<TestBean> = ArrayList()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initList()
        initRv()
    }

    private fun initList() {
        list.add("1")
        list.add("2")
        list.add("3")
        list.add("4")
        list.add("5")

        list2.add(TestBean("1", 0))
        list2.add(TestBean("2", 1))
        list2.add(TestBean("3", 0))
        list2.add(TestBean("4", 1))
        list2.add(TestBean("5", 0))
        list2.add(TestBean("6", 1))
        list2.add(TestBean("7", 2))
        list2.add(TestBean("8", 0))
        list2.add(TestBean("9", 1))
        list2.add(TestBean("10", 2))
    }

    private fun initRv() {
        rvMain.apply {
            layoutManager = LinearLayoutManager(this@MainActivity)
//            layoutManager = GridLayoutManager(this@MainActivity, 4)
            setHasFixedSize(true)
//            adapter = initAdapter()
//            adapter = initAdapter1()
//            adapter = initAdapter5()
//            adapter = initAdapter3()
//            adapter = initAdapter6()
            adapter = initAdapter7()
        }
    }

    private fun initAdapter6(): CommonAdapter<String, ItemABinding> {
        return object : CommonAdapter<String, ItemABinding>(ItemABinding::inflate, this, list) {
            override fun convert(v: ItemABinding, t: String, position: Int) {
                v.tvItme.text = t
            }
        }
    }

    private fun initAdapter7(): MultiItemCommonAdapter<TestBean, ViewBinding> {
        return object : MultiItemCommonAdapter<TestBean, ViewBinding>(this, list2) {
            override fun convert(v: ViewBinding, t: TestBean, position: Int) {
                when (t.type) {
                    0 -> {
                        val v1 = v as ItemMainBinding
                        v.tvItme.text = "0 _${t.name}"
                        v.layoutItem.setOnClickListener {
                            Log.e("点击" , "ItemMainBinding")
                        }
                    }
                    1 -> {
                        val v2 = v as ItemABinding
                        v.tvItme.text = "1 _${t.name}"
                        v.layoutItem.setOnClickListener {
                            Log.e("点击" , "ItemABinding")
                        }
                    }
                    2 -> {
                        val v3 = v as ItemBBinding
                        v.tvItme.text = "2 _${t.name}"
                        v.layoutItem.setOnClickListener {
                            Log.e("点击" , "ItemBBinding")
                        }
                    }
                }
            }

            override fun getItemType(position: Int, t: TestBean): Int {
                return t.type
            }

            override fun getLayout(viewType: Int): (LayoutInflater, ViewGroup, Boolean) -> ViewBinding {
                return when (viewType) {
                    0 -> {
                        ItemMainBinding::inflate
                    }
                    1 -> {
                        ItemABinding::inflate
                    }
                    2 -> {
                        ItemBBinding::inflate
                    }
                    else -> {
                        ItemMainBinding::inflate
                    }
                }
            }
        }
    }

//    private fun initAdapter5(): CommonAdapter<String, ItemABinding> {
//        return object : CommonAdapter<String, ItemABinding>(ItemABinding::inflate,this, list) {
//            override fun convert(v: ItemABinding, t: String, position: Int) {
//                v.tvItme.text = t
//            }
//
//            override fun onLayout(
//                form: LayoutInflater,
//                parent: ViewGroup,
//                viewType: Int
//            ): ViewBinding {
//                return ItemABinding.inflate(form, parent, false)
//            }
//        }
//    }

//    private fun initAdapter(): CommonAdapter<String , ItemMainBinding>{
//        return object : CommonAdapter<String , ItemMainBinding>(this , list){
//            override fun convert(v: ItemMainBinding, t: String, position: Int) {
//                v.tvItme.text = t
//            }
//
//            override fun onLayout(parent: ViewGroup, viewType: Int): ViewBinding {
////                val layoutInflate = LayoutInflater.from(this@MainActivity).inflate(R.layout.item_main , parent , false)
////                return ItemMainBinding.bind(layoutInflate)
//                return ItemMainBinding.inflate(LayoutInflater.from(this@MainActivity) , parent , false)
//            }
//        }
//    }
//
//    private fun initAdapter1(): CommonAdapter<String, ItemABinding> {
//        return object : CommonAdapter<String, ItemABinding>(this, list) {
//            override fun onLayout(parent: ViewGroup,viewType: Int): ViewBinding {
////                return ItemABinding.inflate(LayoutInflater.from(this@MainActivity) , parent , false)
//                return ItemABinding
//            }
//
//            override fun convert(v: ItemABinding, t: String, position: Int) {
//                v.tvItme.text = t
//            }
//
//        }
//    }

//    private fun initAdapter2(): MultiItemCommonAdapter<String, ItemABinding> {
//        return object : MultiItemCommonAdapter<String, ItemABinding>(this, list) {
//            override fun convert(v: ItemABinding, t: String, position: Int) {
//                v.tvItme.text = t
//            }
//
//            override fun getItemType(position: Int, t: String): Int {
//                return 0
//            }
//
//            override fun onLayout(viewType: Int): ViewBinding {
//                return ItemABinding.inflate(layoutInflater)
//            }
//        }
//    }


//    private fun initAdapter3(): MultiItemCommonAdapter<TestBean, ViewBinding> {
//        return object : MultiItemCommonAdapter<TestBean, ViewBinding>(this, list2) {
//            override fun convert(v: ViewBinding, t: TestBean, position: Int) {
//                when(t.type){
//                    0->{
//                        val v1 = v as ItemMainBinding
//                        v.tvItme.text = "0 _${t.name}"
//                    }
//                    1->{
//                        val v2 = v as ItemABinding
//                        v.tvItme.text = "1 _${t.name}"
//                    }
//                    2->{
//                        val v3 = v as ItemBBinding
//                        v.tvItme.text = "1 _${t.name}"
//                    }
//                }
//            }
//
//            override fun getItemType(position: Int, t: TestBean): Int {
//                return t.type
//            }
//
//            override fun onLayout(
//                form: LayoutInflater,
//                parent: ViewGroup,
//                viewType: Int
//            ): ViewBinding {
//                return when (viewType) {
//                    0 -> {
//                        ItemMainBinding.inflate(form, parent, false)
//                    }
//                    1 -> {
//                        ItemABinding.inflate(form, parent, false)
//                    }
//                    2->{
//                        ItemBBinding.inflate(form , parent , false)
//                    }
//                    else -> {
//                        ItemMainBinding.inflate(form, parent, false)
//                    }
//                }
//            }
//        }
//    }
}
